<html>
    <head>
        <title>fetch</title>
        <style>
            .stl{
                width:100px;
                height:100px;
                border:1px solid red;
            }
        </style>
    </head>
<body> 

<h1>Promise 例子3</h1>

<input type="button" onclick="runSimpleAjax()" value="简单的promise"/>
<input type="button" onclick="runAjax()" value="promise依次调用"/>
<input type="button" onclick="runAllAjax()" value="promise.all()"/>



<script>


function request(){
	let p = new Promise(function(resolve,reject){
		fetch(
			"http://127.0.0.1:8000/ajaxserver/SysUser/queryById/402881e846e119dc0146e119e1ed0006",
			{
				'method':'GET',
				headers:{
					'Access-Control-Allow-Origin': '*',
					'Accept': 'application/json',
					'Content-Type': 'application/json',
					'aa':'1234'
				}
			}
		).then(function(res){
			return res.text();
		}).then(function(res){
			resolve(res);
		})
	})
	return p;
}


function runSimpleAjax(){
	request().then(function(data){
		alert(data);
	})
}

function request2(data){
	alert("第二个函数实参:" + data);
	let p = new Promise(function(resolve,reject){
		fetch(
			"http://127.0.0.1:8000/ajaxserver/SysUser/saveSysUser",
			{
				'method':'POST',
				headers:{
					'Access-Control-Allow-Origin': '*',
					'Accept': 'application/json',
					'Content-Type': 'application/json',
					'aa':'1234'
				},
				
				//body 只有POST的方式才可以发送的
				body:JSON.stringify({
						"userName":"a3a2a",
						"userSex":"1",
						"userMail":"aaa@ccc.com",
						"userTel":"1234",
						"userStatus":"1",
						"userBirthday":"2015-01-01",
						"orgId":"5"
				})
			}
		).then(function(res){
			return res.text();
		}).then(function(res){
			resolve(res);
		})
	})
	return p;
}


function runAjax(){
	request().then(request2).then(function(data){
		alert(data);
	})
}



function testGet(){
    return fetch(
        "http://127.0.0.1:8000/ajaxserver/SysUser/queryById/402881e846e119dc0146e119e1ed0006",
        {
         	'method':'GET',
         	headers:{
	         	'Access-Control-Allow-Origin': '*',
	            'Accept': 'application/json',
	            'Content-Type': 'application/json',
	            'aa':'1234'
         	}
        }
    ).then(function(res){
		return res.text();
	})
}
function testPost(){
    return fetch(
        "http://127.0.0.1:8000/ajaxserver/SysUser/saveSysUser",
        {
         	'method':'POST',
         	headers:{
	         	'Access-Control-Allow-Origin': '*',
	            'Accept': 'application/json',
	            'Content-Type': 'application/json',
	            'aa':'1234'
         	},
         	
         	//body 只有POST的方式才可以发送的
        	body:JSON.stringify({
        			"userName":"a3a2a",
        			"userSex":"1",
        			"userMail":"aaa@ccc.com",
        			"userTel":"1234",
        			"userStatus":"1",
        			"userBirthday":"2015-01-01",
        			"orgId":"5"
        	})
        }
    ).then(function(res){
		return res.text();
	})
}

function runAllAjax(){
	const p = Promise.all([testGet(),testPost()]).then(function(resultArray){
		alert(resultArray.length)
		console.log(resultArray)
	});
}


/*
function testGet(){
    fetch(
        "http://127.0.0.1:8000/ajaxserver/SysUser/queryById/402881e846e119dc0146e119e1ed0006",
        {
         	'method':'GET',
         	headers:{
	         	'Access-Control-Allow-Origin': '*',
	            'Accept': 'application/json',
	            'Content-Type': 'application/json',
	            'aa':'1234'
         	}
        }
    ).then(function(res){
		return res.text();
	}).then(function(res){
    	alert(res);
    })
}


function testPost(){
    fetch(
        "http://127.0.0.1:8000/ajaxserver/SysUser/saveSysUser",
        {
         	'method':'POST',
         	headers:{
	         	'Access-Control-Allow-Origin': '*',
	            'Accept': 'application/json',
	            'Content-Type': 'application/json',
	            'aa':'1234'
         	},
         	
         	//body 只有POST的方式才可以发送的
        	body:JSON.stringify({
        			"userName":"a3a2a",
        			"userSex":"1",
        			"userMail":"aaa@ccc.com",
        			"userTel":"1234",
        			"userStatus":"1",
        			"userBirthday":"2015-01-01",
        			"orgId":"5"
        	})
        }
    ).then(function(res){
		return res.text();
	}).then(function(res){
    	alert(res);
    })
}


*/




</script>



</body>
</html>
